package com.sea.finance.service.impl;



import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.VtRelated;
import com.sea.finance.mapper.VtRelatedMapper;
import com.sea.finance.service.VtRelatedService;
import com.sea.finance.service.VtService;
import com.sea.product.entity.Product;
import com.sea.product.service.ProductService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class VtRelatedServiceImpl implements VtRelatedService{
    @Autowired
    private VtRelatedMapper vtRelatedMapper;
    @Autowired
    private VtService vtService;

    @Override
    public Integer save(VtRelated vtRelated) {
        return vtRelatedMapper.insert(vtRelated);
    }

    @Override
    public Integer usave(VtRelated vtRelated) {
        return null;
    }


    @Override
    public List<VtRelated> findByCnd(VtRelated vtRelated) {
        return vtRelatedMapper.findByCnd(vtRelated);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream,String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<VtRelated> list = BatchImportUtils.iteratorForVtRelated(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                clearRelated();
                for (VtRelated vtRelated : list) {
                    List<VtRelated> vList = findByCnd(vtRelated);
                    if(null==vList || vList.size()==0){
                        save(vtRelated);
                    }
                }
                vtService.updateListingName();
            }
        }
    }

    @Override
    public List<VtRelated> findByListingName() {
        return vtRelatedMapper.findByListingName();
    }

    @Override
    public Integer clearRelated() {
        return vtRelatedMapper.clearRelated();
    }
}
